# Maintainer: Biswapriyo Nath <nathbappai@gmail.com>

_realname=neovim
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=0.11.1
pkgrel=1
pkgdesc='Fork of Vim aiming to improve user experience, plugins, and GUIs (mingw-w64)'
arch=('any')
mingw_arch=('ucrt64' 'clang64' 'clangarm64')
url='https://neovim.io/'
msys2_repository_url='https://github.com/neovim/neovim'
msys2_references=(
  'archlinux: neovim'
)
license=('spdx:Apache-2.0')
depends=("${MINGW_PACKAGE_PREFIX}-gettext-runtime"
         "${MINGW_PACKAGE_PREFIX}-libiconv"
         "${MINGW_PACKAGE_PREFIX}-libluv"
         "${MINGW_PACKAGE_PREFIX}-libtermkey"
         "${MINGW_PACKAGE_PREFIX}-libutf8proc"
         "${MINGW_PACKAGE_PREFIX}-libuv"
         "${MINGW_PACKAGE_PREFIX}-libvterm"
         "${MINGW_PACKAGE_PREFIX}-luajit"
         "${MINGW_PACKAGE_PREFIX}-msgpack-c"
         "${MINGW_PACKAGE_PREFIX}-libtree-sitter"
         "${MINGW_PACKAGE_PREFIX}-tree-sitter-c"
         "${MINGW_PACKAGE_PREFIX}-tree-sitter-lua"
         "${MINGW_PACKAGE_PREFIX}-tree-sitter-markdown"
         "${MINGW_PACKAGE_PREFIX}-tree-sitter-query"
         "${MINGW_PACKAGE_PREFIX}-tree-sitter-vim"
         "${MINGW_PACKAGE_PREFIX}-tree-sitter-vimdoc"
         "${MINGW_PACKAGE_PREFIX}-unibilium")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-make"
             "${MINGW_PACKAGE_PREFIX}-lua51-mpack"
             "${MINGW_PACKAGE_PREFIX}-gettext-tools"
             "${MINGW_PACKAGE_PREFIX}-ninja")
source=("${msys2_repository_url}/archive/v${pkgver}/${_realname}-${pkgver}.tar.gz"
        "0001-cmake-disable-bundling.patch")
sha256sums=('ffe7f9a7633ed895ff6adb1039af7516cd6453715c8889ad844b6fa39c3df443'
            '356ecd2d571ef9bc30bdcdc55dc27b39a9283019dadaac748b508cb9f71ea0f5')


prepare() {
  cd "${srcdir}"/${_realname}-${pkgver}
  patch -p1 -i "${srcdir}/0001-cmake-disable-bundling.patch"

  # Werror is enabled in CI only
  sed "s/-Werror/-Wall/g" -i src/nvim/CMakeLists.txt
}

build() {
  mkdir -p "${srcdir}/build-${MSYSTEM}" && cd "${srcdir}/build-${MSYSTEM}"

  declare -a extra_config
  if check_option "debug" "n"; then
    extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  CFLAGS+=" -Wno-format -Wno-format-extra-args"
  CXXFLAGS+=" -Wno-format -Wno-format-extra-args"

  # Build only lpeg statically
  "${MINGW_PREFIX}"/bin/cmake.exe \
      -GNinja \
      -DCMAKE_INSTALL_PREFIX="${MINGW_PREFIX}" \
      -DUSE_BUNDLED=OFF \
      -DUSE_BUNDLED_LPEG=ON \
      "${extra_config[@]}" \
      -B "${srcdir}/${_realname}-${pkgver}/.deps" \
      "${srcdir}/${_realname}-${pkgver}/cmake.deps"

  "${MINGW_PREFIX}"/bin/cmake.exe --build "${srcdir}/${_realname}-${pkgver}/.deps"

  # MSYS2_ARG_CONV_EXCL is necessary so that -DCMAKE_INSTALL_PREFIX is passed correctly
  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    "${MINGW_PREFIX}"/bin/cmake.exe \
      -GNinja \
      -DCMAKE_INSTALL_PREFIX="${MINGW_PREFIX}" \
      "${extra_config[@]}" \
      -DENABLE_TRANSLATIONS=ON \
      ../${_realname}-${pkgver}

  "${MINGW_PREFIX}"/bin/cmake.exe --build "${srcdir}/build-${MSYSTEM}"
}

check() {
  cd "${srcdir}/build-${MSYSTEM}"

  ./bin/nvim.exe --version
  ./bin/nvim.exe --headless -u NONE -i NONE -c ':quit'
}

package() {
  cd "${srcdir}/build-${MSYSTEM}"

  DESTDIR="${pkgdir}" "${MINGW_PREFIX}"/bin/cmake.exe --install .

  install -Dm644 "${srcdir}"/${_realname}-${pkgver}/LICENSE.txt \
    "${pkgdir}"${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE.txt
}
